﻿using Apps.LoggerPlugin.LoggerService;
using QDP.Platform.Services;
using QDP.Platform.Silverlight;

namespace Apps.LoggerPlugin
{
    /// <summary>
    /// 日志写入器
    /// </summary>
    [WidgetMeta(Key = URL_LOG, Desc = "日志记录服务的地址")]
    [WidgetMeta(Key = DEBUG, Desc = "是否记录DEBUG类型日志", DefaultValue = NParams.True)]
    [WidgetMeta(Key = ERROR, Desc = "是否记录ERROR类型日志", DefaultValue = NParams.True)]
    [WidgetMeta(Key = FATAL, Desc = "是否记录FATAL类型日志", DefaultValue = NParams.True)]
    [WidgetMeta(Key = INFO, Desc = "是否记录INFO类型日志", DefaultValue = NParams.True)]
    [WidgetMeta(Key = WARN, Desc = "是否记录WARN类型日志", DefaultValue = NParams.True)]
    public class LogWriterWidget : PartElement, ILoggerService
    {
        const string URL_LOG = "LOG:URL";
        const string DEBUG = "DEBUG";
        const string ERROR = "ERROR";
        const string FATAL = "FATAL";
        const string INFO = "INFO";
        const string WARN = "WARN";
        private LoggerClient client;
        private string username = string.Empty;
        public override void Show()
        {
            IsDebug = !this.GetConfigValue(DEBUG).Equals("false");
            IsError = !this.GetConfigValue(ERROR).Equals("false");
            IsFatal = !this.GetConfigValue(FATAL).Equals("false");
            IsInfo = !this.GetConfigValue(INFO).Equals("false");
            IsWarn = !this.GetConfigValue(WARN).Equals("false");
            this.IsShowed = true;
        }
        public override void Close()
        {
            this.IsDebug = this.IsError = this.IsFatal = this.IsInfo = this.IsWarn = false;
            this.IsShowed = false;
        }
        private void Writer(string appid, string type, string content)
        {
            if (Status == ServiceStatus.Stoped) return; // 当服务处于关闭状态时不可用。
            if (client == null)
            {
                string url = this.GetConfigValue(URL_LOG);
                client = ServiceLoader.CreateWCFClient<LoggerClient>(url);
                client.LogCompleted += new System.EventHandler<LogCompletedEventArgs>(client_LogCompleted);
            }
            dynamic user = Q.DataManager.GetUserData();
            if (user == null)
                username = user.UserCode;
            client.LogAsync(username, type, appid, content);
        }

        void client_LogCompleted(object sender, LogCompletedEventArgs e)
        {

        }
        public void Debug(string appid, string format, params object[] args)
        {
            if (IsDebug)
                Writer(appid, DEBUG, string.Format(format, args));
        }

        public void Error(string appid, string format, params object[] args)
        {
            if (IsError)
                Writer(appid, ERROR, string.Format(format, args));
        }

        public void Fatal(string appid, string format, params object[] args)
        {
            if (IsFatal)
                Writer(appid, FATAL, string.Format(format, args));
        }

        public void Info(string appid, string format, params object[] args)
        {
            if (IsInfo)
                Writer(appid, INFO, string.Format(format, args));
        }

        public void Warn(string appid, string format, params object[] args)
        {
            if (IsWarn)
                Writer(appid, WARN, string.Format(format, args));
        }

        public bool IsDebug { get; set; }

        public bool IsError { get; set; }

        public bool IsFatal { get; set; }

        public bool IsInfo { get; set; }

        public bool IsWarn { get; set; }

        public string Caption
        {
            get { return "SQLSERVER LOG SERVICE"; }
        }

        public string Descript
        {
            get { return "基于SQL Server数据库的日志记录服务。"; }
        }

        public string Handler
        {
            get { return string.Empty; }
        }

        public bool Start()
        {
            this.Show();
            Status = ServiceStatus.Running;
            return true;
        }

        public ServiceStatus Status { get; private set; }

        public bool Stop()
        {
            Status = ServiceStatus.Stoped;
            this.Close();
            return true;
        }
    }
}
